import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter_taotaoshipin/widget/my_app_bar.dart';
import 'package:flutter_taotaoshipin/widget/refresh_header.dart';
import 'package:flutter_taotaoshipin/widget/refresh_status_view.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'purchase_record_controller.dart';

class PurchaseRecordPage extends StatefulWidget {
  const PurchaseRecordPage({Key? key}) : super(key: key);

  @override
  State<PurchaseRecordPage> createState() =>
      _PurchaseRecordPageState();
}

class _PurchaseRecordPageState extends State<PurchaseRecordPage> {
  final controller = Get.find<PurchaseRecordController>();
  final state = Get.find<PurchaseRecordController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: '充值记录',
      ),
      body: RefreshStatusView(
        controller: controller,
        contentBuilder: (_) {
          return SmartRefresher(
            controller: controller.refreshController,
            // 下拉刷新
            enablePullDown: true,
            header: const RefreshHeader(),
            onRefresh: controller.onRefresh,
            child: CustomScrollView(
              slivers: [
                _buildPayLogListView(),
              ],
            ),
          );
        },
      ),
    );
  }

  _buildPayLogListView() {
    return SliverGrid.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 1,
        mainAxisSpacing: 9,
        crossAxisSpacing: 9,
        mainAxisExtent: 76,
      ),
      itemBuilder: (context, index) {
        var model = state.payLogList[index];
        bool paySuccess = model.status!.contains('成功');
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 17),
          padding: EdgeInsets.fromLTRB(24, 0, 22, 0),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
          ),
          child: Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '${MoneyUtil.changeYWithUnit(model.amount!, MoneyUnit.YUAN_ZH, format: MoneyFormat.END_INTEGER)}',
                      style: TextStyle(
                        color: Color(0xFF333333),
                        fontSize: 16,
                        fontWeight: FontWeight.w900,
                      ),
                    ),
                    SizedBox(height: 5),
                    Text(
                      DateUtil.formatDateMs(
                        model.ctime! * 1000,
                        format: DateFormats.full,
                      ),
                      style: TextStyle(color: Color(0xFFB4B3B8), fontSize: 12),
                    ),
                  ],
                ),
              ),
              Text(
                model.status ?? '',
                style: TextStyle(
                  color: paySuccess ? Color(0xFF66CD46) : Color(0xFFE54C4C),
                  fontSize: 16,
                  fontWeight: FontWeight.w900,
                ),
              ),
            ],
          ),
        );
      },
      itemCount: state.payLogList.length,
    );
  }
}
